class Solution
{
public:
    int scoreOfParentheses(string s)
    {
        return scoreOfParentheses(s, 0, s.size());
    }

    int scoreOfParentheses(string &s, int left, int right) // 包括left，不包括right
    {
        int count = 0;
        int score = 0;
        for (int i = left; i < right; ++i)
        {
            if (s[i] == '(')
            {
                ++count;
            }
            else if (s[i] == ')')
            {
                --count;
                if (count == 0)
                {
                    if (left + 1 == i)
                    {
                        score += 1;
                    }
                    else
                    {
                        score += 2 * scoreOfParentheses(s, left + 1, i);
                    }
                    left = i + 1;
                }
            }
        }
        return score;
    }
};